module.exports = app => {
  const router = require('express').Router();
  const mongoose = require('mongoose');
  const Article = mongoose.model('Article');
  const Category = mongoose.model('Category');
  const Hero = mongoose.model('Hero'); 

  // 导入新闻数据
  router.get('/news/init', async(req, res) => {
    const parent = await Category.findOne({
      name: '新闻分类'
    });
    const cates = await Category.find().where({
      parent: parent
    }).lean();
    const newsTitles = ["夏日新版本“星之队”即将在今年的6月份和大家详见", "标题2", "标题3", "标题4", "标题5", "标题6", "标题7", "标题8"]; 
    const newsList = newsTitles.map( title => {
      const randomCates = cates.slice(0).sort((a, b) => Math.random() - 0.5)
      return {
        categories: randomCates.slice(0, 2),
        title: title
      }
    })
    await Article.deleteMany({})
    await Article.insertMany(newsList)
    res.send(newsList)
  })

  // 获取文章数据
  router.get('/news/list', async(req, res) => {
    // const parent = await Category.findOne({
    //   name: '新闻分类'
    // }).populate({
    //   path: 'children',
    //   populate: {
    //     path: 'newsList',
    //   }
    // }).lean();

    // 聚合查寻 aggregate
    const parent = await Category.findOne({
      name: '新闻分类'
    })
    const cates = await Category.aggregate([
      { $match: {parent: parent._id} },
      { $lookup: {
        from: 'articles',
        localField: '_id',
        foreignField: 'categories',
        as: 'newsList'
      }},
      {
        $addFields: {
          newsList: { $slice: ['$newsList', 5] }
        }
      }
    ])
    const subCates = cates.map(v => v._id);
    cates.unshift({
      name: '热门',
      newsList: await Article.find().where({
        categories: { $in: subCates }
      }).populate('categories').limit(5).lean()
    })

    cates.map(cate => {
      cate.newsList.map(news => {
        news.categoryName = cate.name === '热门' ? news.categories[0].name : cate.name
        return news
      })
      return cate
    })
    res.send(cates);
  })


  //  导入英雄数据
  // router.get('/heros/init', async(req, res) => {
  //   const rawData = [{"name": "热门", "heroes": [
  //     {"name:": "后羿", "avatar": "fasdasasa"}
  //   ]}]
  // })

  // 获取英雄列表数据
  router.get('/heros/list', async(req, res) => {
    // const parent = await Category.findOne({
    //   name: '新闻分类'
    // }).populate({
    //   path: 'children',
    //   populate: {
    //     path: 'newsList',
    //   }
    // }).lean();

    // 聚合查寻 aggregate
    const parent = await Category.findOne({
      name: '英雄列表'
    })
    const cates = await Category.aggregate([
      // 查找 all
      { $match: {parent: parent._id} },
      // 关联查询
      { $lookup: {
        from: 'heros',
        localField: '_id',
        foreignField: 'categories',
        as: 'heroList'
      }}
    ])
    const subCates = cates.map(v => v._id);
    cates.unshift({
      name: '热门',
      heroList: await Hero.find().where({
        categories: { $in: subCates }
      }).limit(10).lean()
    })
    res.send(cates);
  })


  // 文章详情页
  router.get('/articles/:id', async(req, res) => {
    const data = await Article.findById(req.params.id).lean();
    data.related = await Article.find().where({
      // 在相同分类下查找
      categories: { $in: data.categories }   
    }).limit(2)
    res.send(data);
  })

  // 获取单个英雄详情信息
  router.get('/heros/:id', async(req, res) => {
    const data = await Hero.findById(req.params.id).populate('categories').lean();
    res.send(data);
  })

  app.use('/web/api', router);
}